/*
 * Punto.java
 *
 * Created on 17 de mayo de 2008, 09:25 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package futbol.forrest.gemetry;
import java.awt.Point;

/**
 *
 * @author Pablo
 */
public class VecPosition {
    
    public enum CoordSystemT {
      CARTESIAN,
      POLAR
    }
    
    private double m_x;   /*!< x-coordinate of this position */
    private double m_y;   /*!< y-coordinate of this position */    
    
    /** Creates a new instance of Punto */
    public VecPosition(){
        this.m_x = 0;
        this.m_y = 0;
    }

    public VecPosition(double x, double y){
        setVecPosition( x, y);
    }
    
    public VecPosition(double x, double y, CoordSystemT cs){
        setVecPosition( x, y, cs );
    }

    public VecPosition(Point p){
        this.m_x = p.getX();
        this.m_y = p.getY();
    }
    
    public VecPosition(VecPosition p){
        this.m_x = p.getX();
        this.m_y = p.getY();
    }    

    public VecPosition sum(VecPosition p){
        return  new VecPosition( m_x + p.m_x, m_y + p.m_y );
    }
          
    public VecPosition subtract(VecPosition p){
        return new VecPosition(m_x - p.m_x, m_y - p.m_y);        
    }
    
    public double getDistanceTo(VecPosition p){
        return subtract(p).getMagnitude();
    }

    public double getMagnitude(){
        return Math.sqrt(m_x * m_x + m_y * m_y );
    }
    
    public void setMagnitude(double d){
        if(this.getMagnitude() > Geometry.EPSILON){            
            m_x *= d / getMagnitude();
            m_y *= d / getMagnitude();
        }             
    }

    public void setVecPosition(double x, double y){
        m_x = x;
        m_y = y;
    }
    
    public void setVecPosition(double x, double y, CoordSystemT cs){
        if( cs == CoordSystemT.CARTESIAN){
            setVecPosition(x,y);
        }else{
            VecPosition temp = getVecPositionFromPolar( x, y);
            this.m_x = temp.m_x;
            this.m_y = temp.m_y;
        }
    }
       
    /*! This method converts a polar representation of a VecPosition into
        a Cartesian representation.

        \param dMag a double representing the polar r-coordinate, i.e. the
        distance from the point to the origin

        \param ang the angle that the polar vector makes with the x-axis,
        i.e. the polar phi-coordinate

        \return the result of converting the given polar representation
        into a Cartesian representation thus yielding a Cartesian
        VecPosition */
    public static VecPosition getVecPositionFromPolar( double dMag, double ang ){
      // cos(phi) = x/r <=> x = r*cos(phi); sin(phi) = y/r <=> y = r*sin(phi)
      return new VecPosition( dMag * Geometry.cosDeg( ang ), dMag * Geometry.sinDeg( ang ) ) ;
    }

    
    /*! This method normalizes an angle. This means that the resulting
        angle lies between -180 and 180 degrees.

        \param angle the angle which must be normalized

        \return the result of normalizing the given angle */
    public static double normalizeAngle( double angle ){
      while( angle > 180.0  ) angle -= 360.0;
      while( angle < -180.0 ) angle += 360.0;
      return ( angle );
    }

    public double getDirection(){
        return Geometry.atan2Deg(m_y, m_x);
    }          
  
    public double getX() {
        return m_x;
    }

    public void setX(double m_x) {
        this.m_x = m_x;
    }

    public double getY() {
        return m_y;
    }

    public void setY(double m_y) {
        this.m_y = m_y;
    }
    
    

}
